﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Log
{
    /// <summary>
    /// 自定义之日助手
    /// </summary>
    public sealed class LogHelper
    {
        #region 单利模式
        //创建私有静态字段，接收类的实例化对象  
        private static readonly LogHelper _LogHelper = null;
        //构造函数私有化  
        private LogHelper() { }
        //静态构造函数，创建单利对象资源  
        static LogHelper()
        {
            _LogHelper = new LogHelper();
        }

        //获取单利对象资源  
        public static LogHelper GetSingleObj()
        {
            return _LogHelper;
        }
        #endregion

        /// <summary>
        /// 日志等级
        /// </summary>
        private enum LogType { ERROR = 1, INFO = 2, DEBUG = 3 };

        /// <summary>
        /// 指定日志文件夹（项目跟路劲文件夹）
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 向日志文件写入调试信息
        /// </summary>
        /// <param name="logName">类名/方法名</param>
        /// <param name="logContent">日志记录内容</param>
        public void Debug(string logName, string logContent)
        {
            WriteLog(new LogParameter() { LogGrade = (int)LogType.DEBUG, LogType = nameof(LogType.DEBUG), LogName = logName, LogContent = logContent });
        }

        /// <summary>
        /// 向日志文件写入运行时信息
        /// </summary>
        /// <param name="logName">类名/方法名</param>
        /// <param name="logContent">日志记录内容</param>
        public void Info(string logName, string logContent)
        {
            WriteLog(new LogParameter() { LogGrade = (int)LogType.INFO, LogType = nameof(LogType.INFO), LogName = logName, LogContent = logContent });
        }

        /// <summary>
        /// 向日志文件写入出错信息
        /// </summary>
        /// <param name="logName">类名/方法名</param>
        /// <param name="logContent">日志记录内容</param>
        public void Error(string logName, string logContent)
        {
            WriteLog(new LogParameter() { LogGrade = (int)LogType.ERROR, LogType = nameof(LogType.ERROR), LogName = logName, LogContent = logContent });
        }

        /// <summary>
        /// 实际的写日志操作
        /// </summary>
        /// <param name="logParameter">日志参数model</param>
        private void WriteLog(LogParameter logParameter)
        {
            if (!Directory.Exists(Path))//如果日志目录不存在就创建
            {
                Directory.CreateDirectory(Path);
            }

            DateTime dateTime = DateTime.Now;
            string time = dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
            //string filename = Path + "/" + dateTime.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
            string filename = $"{Path}/{dateTime.ToString("yyyy-MM-dd")}.log";//同上等效

            #region 原始写法
            //创建或打开日志文件，向日志文件末尾追加记录
            //StreamWriter mySw = File.AppendText(filename);

            //向日志文件写入内容
            //string writeContent = time + "|" + typeGrade + ":" + type + "|" + className + ":" + content;
            //mySw.WriteLine(writeContent);

            //关闭日志文件
            //mySw.Close(); 
            #endregion

            //（优化写法）创建或打开日志文件，向日志文件末尾追加记录,关闭日志文件
            using (StreamWriter mySw = File.AppendText(filename))
            {
                string writeContent = $"{time}|{logParameter.LogGrade}:{logParameter.LogType}|{logParameter.LogName}:{logParameter.LogContent}";
                mySw.WriteLine(writeContent);//向日志文件写入内容
                mySw.Close(); //关闭日志文件
            }
        }
    }
}
